package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 菜品控制层
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    private final DishService dishService;
    private final RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    public DishController(DishService dishService, RedisTemplate<Object, Object> redisTemplate) {
        this.dishService = dishService;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 新增菜品
     *
     * @param dishDTO 菜品dto
     * @return 提示
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        cleanCache("dish_" + dishDTO.getCategoryId());
        return Result.success(MessageConstant.ADD_OK);
    }


    /**
     * 分页查询菜品列表
     *
     * @param queryDTO 查询实体
     * @return 菜品列表
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult<DishVO>> pageDishList(DishPageQueryDTO queryDTO) {
        log.info("菜品分页查询:{}", queryDTO);
        return Result.success(dishService.pageDishList(queryDTO), MessageConstant.SELECT_OK);
    }


    /**
     * 菜品批量删除
     *
     * @param ids 有可能是 多条数据
     * @return 提示
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result<String> deletes(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);
        //将所有的菜品缓存清理掉 以dish_下划线开头的
        cleanCache("dish_*");
        return Result.success(MessageConstant.OPERATE_OK);
    }

    /**
     * 菜品起售、停售
     *
     * @param status 菜品状态 1起售 0禁售
     * @param id     菜品id
     * @return 提示
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售、停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("菜品起售、停售:{},{}", status, id);
        dishService.startOrStop(status, id);
        //将所有的菜品缓存清理掉 以dish_下划线开头的
        cleanCache("dish_*");
        return Result.success(MessageConstant.UPDATE_OK);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO 修改菜品实体
     * @return 提示
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO);
        dishService.updateDish(dishDTO);
        //将所有的菜品缓存清理掉 以dish_下划线开头的
        cleanCache("dish_*");
        return Result.success(MessageConstant.UPDATE_OK);
    }

    /**
     * 单个菜品详细信息
     *
     * @param id 菜品id
     * @return 菜品vo
     */
    @GetMapping("/{id}")
    @ApiOperation("单个菜品详细信息")
    public Result<DishVO> dishDetail(@PathVariable Long id) {
        log.info("单个菜品详细信息:{}", id);
        return Result.success(dishService.getDishDetailById(id), MessageConstant.SELECT_OK);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId 分类id
     * @return 分类下的菜品列表
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        log.info("根据分类id查询菜品:{}", categoryId);
        return Result.success(dishService.list(categoryId), MessageConstant.SELECT_OK);
    }


    /**
     * 统一清理缓存数据
     *
     * @param pattern 模式
     */
    private void cleanCache(String pattern) {
        redisTemplate.delete(Objects.requireNonNull(redisTemplate.keys(pattern)));
    }
}
